﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Net;
using System.IO;

namespace GLDownloadDocs
{
	class Program
	{
		static void Main(string[] args)
		{
			string[] rootUrls = new string[]
			{
				"https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man2/",
				"https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man3/",
				"https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man4/"
			};

			foreach (string rootUrl in rootUrls)
			{
				Console.WriteLine(rootUrl);

				WebRequest rootRequest = WebRequest.Create(rootUrl);
				WebResponse rootResponse = rootRequest.GetResponse();

				using (StreamReader sr = new StreamReader(rootResponse.GetResponseStream()))
				{
					string line;
					while ((line = sr.ReadLine()) != null)
					{
						line = line.Trim();
						if (line.StartsWith("<file name=\""))
						{
							line = line.Substring(12);
							line = line.Substring(0, line.IndexOf('"'));

							if (line.EndsWith(".xml") && line.StartsWith("gl"))
							{
								Console.WriteLine(line);

								WebRequest request = WebRequest.Create(rootUrl + "/" + line);
								WebResponse response = request.GetResponse();

								using (StreamReader sr2 = new StreamReader(response.GetResponseStream()))
								{
									File.WriteAllText(line, sr2.ReadToEnd());
								}
							}
						}
					}
				}
			}

			Console.WriteLine("Press any key to exit...");
			Console.ReadKey();
		}
	}
}
